// 当前验证码
var randomCode = null;

// 找回密码
$("#registerBtn").on("click", function () {
    var userEmail = $(".userEmail").val();
    var userPassword = $(".userPassword").val();
    var verificationCode = parseInt($(".verificationCode").val());
    var userPasswordCheck = $(".userPasswordCheck").val();
    var reg_userPassword = /^([0-9]|[a-zA-Z]){6,16}$/;
    var reg_userEmail =/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
    if (userEmail != null && userEmail.length != 0 && reg_userEmail.test(userEmail)) {
        if (userPassword != null && userPassword.length != 0 && reg_userPassword.test(userPassword)) {
            if (userPasswordCheck != null && userPasswordCheck.length != 0) {
                if (userPassword == userPasswordCheck) {
                    if (verificationCode != null && verificationCode.length != 0) {
                        if (verificationCode == randomCode) {
                            $.ajax({
                                type: "POST",
                                url: "/getBackPassword",
                                data: {userEmail: userEmail, userPassword: userPassword},
                                dataType: "json",
                                success: function (data) {
                                    if (data.result == "success") {
                                        $(".getback-message").html("密码修改成功").css({"color": "green"});
                                    }
                                },
                                error: function () {
                                    $(".getback-message").html("服务器错误，操作失败").css({"color": "red"});
                                }
                            });
                        } else {
                            $(".getback-message").html("邮箱验证码错误").css({"color": "red"});
                        }
                    } else {
                        $(".getback-message").html("请输入验证码").css({"color": "red"});
                    }
                } else {
                    $(".getback-message").html("两次输入的密码不同，请重新输入").css({"color": "red"});
                }
            } else {
                $(".getback-message").html("请再次输入密码").css({"color": "red"});
            }
        } else {
            $(".getback-message").html("新密码不能为空，且满足6~16位数字、字母").css({"color": "red"});
        }
    } else {
        $(".getback-message").html("邮箱不能为空，格式：123Test@qq.com").css({"color": "red"});
    }
});

// 获得验证码
$("#GetVerificationCode").on("click", function () {
    var userEmail = $(".userEmail").val();
    var reg_userEmail =/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
    if (userEmail != null && userEmail.length != 0 && reg_userEmail.test(userEmail)) {
        $.ajax({
            type: "POST",
            url: "/getJavaMailSender",
            data: {userEmail: userEmail},
            dataType: "json",
            success: function(data) {
                if (data.message == null) {
                    randomCode = data.code;
                    $(".getback-message").html("验证码已发送到邮箱").css({"color": "green"});
                } else {
                    $(".getback-message").html(data.message).css({"color": "red"});
                }
            },
            error: function () {
                $(".getback-message").html("ajax 服务器错误").css({"color": "red"});
            }
        });
    } else {
        $(".getback-message").html("邮箱不能为空，格式：123Test@qq.com").css({"color": "red"});
    }
});

// 登录
$(".loginFormBtn").on("click", function () {
    var userName = $(".userName").val();
    var userPassword = $(".userPassword").val();
    if (userName.length == 0 || userName == null || userPassword.length == 0 || userPassword == null) {
        alert("用户名或者密码不能为空");
    } else {
        $.ajax({
            type: "POST",
            url: "/login",
            data: $("#loginForm").serialize(),
            dataType: "json",
            success: function(data) {
                if (data.message == "success") {
                    if (data.url != null) {
                        window.location.href=data.url;
                    } else {
                        window.location.href="/index";
                    }
                } else {
                    $(".login-message").html(data.message).css({"color": "red"});
                }
            },
            error: function () {
                $(".login-message").html("ajax 服务器错误").css({"color": "red"});
            }
        });
    }
});

// 注册
$("#registerBtn").on("click", function () {
    var userName = $(".userName").val();
    var userEmail = $(".userEmail").val();
    var userPhone = $(".userPhone").val();
    var userPassword = $(".userPassword").val();
    var userPasswordCheck = $(".userPasswordCheck").val();
    var reg_userPassword = /^([0-9]|[a-zA-Z]){6,16}$/;
    var reg_userPhone = /^1[345678]\d{9}$/;
    var reg_userEmail =/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
    if (userName != null && userName.length != 0) {
        if (userEmail != null && userEmail.length != 0 && reg_userEmail.test(userEmail)) {
            if (userPhone != null && userPhone.length != 0 && reg_userPhone.test(userPhone)) {
                if (userPassword != null && userPassword.length != 0 && reg_userPassword.test(userPassword)) {
                    if (userPasswordCheck != null && userPasswordCheck.length != 0) {
                        if (userPassword == userPasswordCheck) {
                            $.ajax({
                                type: "POST",
                                url: "/checkUserOnly?userName=" + userName + "&userEmail=" + userEmail,
                                dataType: "json",
                                success: function (data) {
                                    if (data.result == null) {
                                        $.ajax({
                                            type: "POST",
                                            url: "/register",
                                            data: $("#registerForm").serialize(),
                                            dataType: "json",
                                            success: function (data) {
                                                if (data.result == "success") {
                                                    $(".register-message").html("注册成功").css({"color": "green"});
                                                    $("#registerForm")[0].reset();
                                                }
                                            },
                                            error: function () {
                                                $(".register-message").html("ajax 服务器错误").css({"color": "red"});
                                            }
                                        });
                                    } else {
                                        $(".register-message").html(data.result).css({"color": "red"});
                                    }
                                },
                                error: function () {
                                    $(".register-message").html("服务器错误，操作失败").css({"color": "red"});
                                }
                            });
                        } else {
                            $(".register-message").html("两次输入的密码不同，请重新输入").css({"color": "red"});
                        }
                    } else {
                        $(".register-message").html("请再次输入密码").css({"color": "red"});
                    }
                } else {
                    $(".register-message").html("密码不能为空，且满足6~16位数字、字母").css({"color": "red"});
                }
            } else {
                $(".register-message").html("电话号码不能为空，支持11位手机号码").css({"color": "red"});
            }
        } else {
            $(".register-message").html("邮箱不能为空，格式：123Test@qq.com").css({"color": "red"});
        }
    } else {
        $(".register-message").html("用户名不能为空").css({"color": "red"});
    }
});
